package com.redshark.wmm.MeasurementProcessing;

import android.util.Log;

public class CurrentProcessor implements MeasurementProcessor {
	
	public static final int LOW_CURRENT = 0;
	public static final int HIGH_CURRENT = 1;
	
	private int count = 0;
	private double total = 0;

	public static double processMeasurement(int ADReading, int channel) {
		double value = AD_CONSTANT * ADReading;

		switch (channel) {
		case LOW_CURRENT:
			value *= 202.71;
			value -= 512.59;
			break;
		case HIGH_CURRENT:
			value *= 9.9194;
			value -= 25.155;
			break;
		}
		return value;
	}

	@Override
	public Measurement displayMeasurement(int[] data) {
		//return new Measurement(data[0]*AD_CONSTANT, new DecimalFormat("###,###.0000"), new DecimalFormat("000,000.000").format(data[1]*AD_CONSTANT));
		
		Log.v("CurrentProcessor", "Process Low: " + data[0] + " High: " + data[1]);
		double current;
		//Our calibration centers the low at 509/510, high at 513
		if(data[0] > 513 || data[0]<508){
			 current = processMeasurement(data[0], 0);
		}
		else{
			current = processMeasurement(data[1], 1);
		}

		
		count++;
		total+=current;

		if(count > 4){
			current = total/count;

			count = 0;
			total = 0;
			
			Measurement m;
			if(current < 1){
				current*=1000;
				m = new Measurement(current, 3, "mA");
			}
			else{
				m = new Measurement(current, 3, "A");
			}
			return m;
		}
		else{
			return null;
		}
				
	}

}
